package cn.lz.util;

import org.apache.http.util.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * @author: TT
 * @date: 2018/12/22
 * @time: 16:08
 * @version: V1.0
 */
public class DateUtils {
    /**
     *      * Java将Unix时间戳转换成指定格式日期字符串
     *      * @param timestampString 时间戳 如："1473048265";
     *      * @param formats 要格式化的格式 默认："yyyy-MM-dd HH:mm:ss";
     *      *
     *      * @return 返回结果 如："2016-09-05 16:06:42";
     *      
     */
    public static String TimeStamp2Date(String timestampString, String formats) {
        if (TextUtils.isEmpty(formats))
            formats = "yyyy-MM-dd HH:mm:ss";
        Long timestamp = Long.parseLong(timestampString) * 1000;
        String date = new SimpleDateFormat(formats, Locale.CHINA).format(new Date(timestamp));
        return date;
    }

    /**
     *  
     *      * 将时间字符串转换为Date类型 
     *      * @param dateStr 
     *      * @return Date 
     *      
     */
    public static Date toDate(String dateStr) {
        Date date = null;
        SimpleDateFormat formater = new SimpleDateFormat();
        formater.applyPattern("yyyy-MM-dd HH:mm:ss");
        try {
            date = formater.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    //  获取当前时间戳
    public static String UnixDate(Date date) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = sdf.format(date);
            return String.valueOf(sdf.parse(format).getTime() / 1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }


    public static void main(String[] args) throws ParseException {

        TimeStamp2Date("1512800755", "yyyy-MM-dd HH:mm:ss");
        System.out.println(TimeStamp2Date("1512800755", "yyyy-MM-dd HH:mm:ss"));
        String string = "2016-09-05 16:06:42";
        Date date = toDate(string);
        System.out.println(date);
        String unixDate = UnixDate(date);
        System.out.println("00" + unixDate + "0000");

    }


}
